import time

from selenium import webdriver as web
from appium import webdriver as app


class Driver:
    # app驱动
    __app_driver = None
    # mis驱动
    __mis_driver = None
    # mp驱动
    __mp_driver = None

    @classmethod
    def get_app_driver(cls):
        """声明手机驱动"""
        if cls.__app_driver is None:
            desired_caps = dict()
            desired_caps['platformName'] = 'Android'
            desired_caps['platformVersion'] = '5.1'
            desired_caps['deviceName'] = '192.168.56.101:5555'
            desired_caps['appPackage'] = 'com.itcast.toutiaoApp'
            desired_caps['appActivity'] = '.MainActivity '
            desired_caps['noReset'] = True
            cls.__app_driver = app.Remote('http://localhost:4723/wd/hub', desired_caps)
            return cls.__app_driver
        else:
            """__app_driver不为None时，说明APP已经被打开"""
            return cls.__app_driver

    @classmethod
    def quit_app_driver(cls):
        """退出手机驱动"""
        if cls.__app_driver:
            cls.__app_driver.quit()
            cls.__app_driver = None

    @classmethod
    def get_mp_driver(cls):
        """声明自媒体驱动"""
        if cls.__mp_driver is None:
            # 声明驱动
            cls.__mp_driver = web.Chrome()
            # 最大化浏览器
            cls.__mp_driver.maximize_window()
            # 访问自媒体首页
            cls.__mp_driver.get("http://ttmp.research.itcast.cn/")
            return cls.__mp_driver
        else:

            return cls.__mp_driver

    @classmethod
    def quit_mp_driver(cls):
        """退出自媒体驱动"""
        if cls.__mp_driver:
            cls.__mp_driver.quit()
            cls.__mp_driver = None

    @classmethod
    def get_mis_driver(cls):
        """声明自媒体驱动"""
        if cls.__mis_driver is None:
            # 声明驱动
            cls.__mis_driver = web.Chrome()
            # 最大化浏览器
            cls.__mis_driver.maximize_window()
            # 访问自媒体首页
            cls.__mis_driver.get("http://ttmis.research.itcast.cn/")
            return cls.__mis_driver
        else:

            return cls.__mis_driver

    @classmethod
    def quit_mis_driver(cls):
        """退出自媒体驱动"""
        if cls.__mis_driver:
            cls.__mis_driver.quit()
            cls.__mis_driver = None


